package com.util;

import com.alibaba.fastjson.JSONObject;
import com.mgr.UserOrderMgr;
import com.qy.bean.OrderInfo;
import com.qy.util.EncryUtil;
import com.qy.util.HttpRequest;
import com.qy.util.StaticData;
import com.qy.util.SystemParamsUtil;

/**
 * baoshang工具类
 * @author zwl
 * 2018年2月6日
 */
public class BaoShangCarUtil {

    /**
     * 用订单id生成二维码
     * @param orderId
     * @return
     * @throws Exception
     */
    public static String createOrderQR(String key) throws Exception{
	String imagePath = null;
	String url = SystemParamsUtil.IMG_SERVICE_URL()+SystemParamsUtil.SERVICE_NAME();
	JSONObject js = new JSONObject();
	js.put(StaticData.reKey.code.getKey(), StaticData.codeType.ok.getType());
	js.put(StaticData.reKey.cmd.getKey(),getQRImageName(key));
	String param = "param="+EncryUtil.encrypt(js.toJSONString());
	String reStr = HttpRequest.sendPost(url, param);
//	reStr = EncryUtil.decrypt(reStr);
	js = JSONObject.parseObject(reStr);
	if(js.containsKey("imagePath")){
	    imagePath = js.getString("imagePath");
	    imagePath= SystemParamsUtil.QR_IMAGE_PATH()+EncryUtil.decrypt(imagePath);
	}
	return imagePath;
    }
    
    public static String getQRImageName(String key) throws Exception{
	return EncryUtil.encrypt(key)+".png";
    }
    
    /**
     * 图片名字获取订单信息
     * @param imageName
     * @return
     * @throws Exception
     */
    public static OrderInfo ImageNameToOrder(String imageName)  throws Exception{
	OrderInfo info = null;
	int orderId = StaticData.no_param;
	int orderStatus =  StaticData.no_param;
	imageName = imageName.replace(".png", "");
	String tmp = EncryUtil.decrypt(imageName);
	if(tmp!=null&&!tmp.isEmpty()){
	    String[] tmps = tmp.split(StaticData.first_split);
	    if(tmps!=null&&tmps.length==2){
		orderId = Integer.parseInt(tmps[0]);
		orderStatus= Integer.parseInt(tmps[1]);
	    }
	}
	OrderInfo orderTmp = UserOrderMgr.getOrderInfo(orderId);
	if(orderTmp!=null
		&&orderStatus!=StaticData.no_param
		&&orderTmp.getOrderStatus()==orderStatus){
	    info = orderTmp;
	}
	return info;
    }
    
    /**
     * 生成二维码的图片
     * @param oderInfo
     * @return
     */
    public static String getImageKey(OrderInfo oderInfo){
	return oderInfo.getId()+StaticData.first_split+oderInfo.getOrderStatus();
    }
}
